package cn.gok.web;


import cn.gok.entity.TStudent;
import cn.gok.service.SecurityQuestionService;
import cn.gok.service.StudentService;
import cn.gok.service.impl.SecurityQuestionServiceImpl;
import cn.gok.service.impl.StudentServiceImpl;
import cn.gok.util.R;
import cn.gok.util.TokenCashUtil;
import cn.hutool.json.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;


@WebServlet("/studentChange")
public class StudentChangeServlet extends HttpServlet {
    private StudentService studentService = new StudentServiceImpl();
    private SecurityQuestionService securityQuestionService=new SecurityQuestionServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        switch (action) {
            case "getStudentInfo":getStudentInfo(req, resp); break;//获取学生个人信息
            case "updateStudentInfo":updateStudentInfo(req, resp); break;//修改学生信息
            case "checkPassword":checkPassword(req, resp);break;//修改密码
            case "updatePassword":updatePassword(req, resp);break;//修改密码
            case "checkStudentName":checkStudentName(req, resp);break;//检查用户名是否重复
            case "checkPhone":checkPhone(req, resp); break;//检查手机号是否重复
            case "getAllQuestion":showAllQuestion(req,resp);break;
            case "addSecurityQuestionAndAnswerInfo":addSecurityQuestionAndAnswerInfo(req, resp);break;
            case "forget":getStudentQuestion(req,resp);break;//通过账号获取密保问题
            case "forget1":getStudentQuestion1(req,resp);break;
            case "checkQuestionAndAnswer":checkQuestionAndAnswer(req,resp);break;
            case "checkStudentName1":checkStudentName1(req, resp);break;//检查用户名是否重复
            case "checkPhone1":checkPhone1(req, resp); break;//检查手机号是否重复


        }
    }

    /*
     *  获取用户的基础信息
     * */
    public void getStudentInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        TStudent student =(TStudent) req.getSession().getAttribute("student");
        R data = R.create(student);
        resp.getWriter().println(JSONUtil.toJsonStr(data));
    }

    /*
     *  检查用户名是否重复
     * */
    public void checkStudentName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        TStudent student =(TStudent) req.getSession().getAttribute("student");
        Integer stuId = student.getStuId();
        String name = req.getParameter("name");
        R r = studentService.queryStudentByStudentName(name,stuId);
        String str = JSONUtil.toJsonStr(r);
        resp.getWriter().println(str);

    }

    /*
     *  检查手机号是否重复
     * */
    public void checkPhone(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        TStudent student =(TStudent) req.getSession().getAttribute("student");
        Integer stuId = student.getStuId();
        String phone = req.getParameter("phone");
        R r = studentService.queryStudentByPhone(phone,stuId);
        String str = JSONUtil.toJsonStr(r);
        resp.getWriter().println(str);

    }

    /*
     *  修改个人信息
     * */
    public void updateStudentInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String,String[]> parameterMap=req.getParameterMap();



        TStudent student =(TStudent) req.getSession().getAttribute("student");
        Integer stuId = student.getStuId();
        String name = req.getParameter("name");
        String real_name = req.getParameter("real_name");
        int age = Integer.parseInt(req.getParameter("age"));
        String gender = req.getParameter("gender");
        String phone = req.getParameter("phone");
        String school = req.getParameter("school");
        R r = studentService.updateStudentInfo(stuId,name,real_name,age,gender,phone,school);
        String str = JSONUtil.toJsonStr(r);
        resp.getWriter().println(str);
    }
    //查询之前的密码
    public void checkPassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        TStudent student = (TStudent) req.getSession().getAttribute("student");
        String password = req.getParameter("password");
        R r = studentService.queryPasswordByPassword(password, student.getStuId());
        resp.getWriter().println(JSONUtil.toJsonStr(r));

    }



    /*
     * 修改密码
     * */
    public void updatePassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        TStudent student =(TStudent) req.getSession().getAttribute("student");
        Integer stuId = student.getStuId();
        String password = req.getParameter("password");
        R r = studentService.updatePasswordByPassword(password, stuId);
        String str = JSONUtil.toJsonStr(r);
        resp.getWriter().println(str);
    }

    public void showAllQuestion(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        R allQuestion = securityQuestionService.getAllQuestion();


        resp.setHeader("Content-type", "application/json;charset=utf-8");
        resp.setContentType("application/json;charset=utf-8");

        resp.getWriter().println(JSONUtil.toJsonStr(allQuestion));
    }


    /**
     * 添加学生的密保问题及答案
     */
    public void addSecurityQuestionAndAnswerInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        TStudent student = (TStudent) req.getSession().getAttribute("student");
        String question = req.getParameter("question");
         String answer = req.getParameter("answer");
        R r = studentService.addSecurityQuestionAndAnswerInfo(student.getStuId(), question, answer);
        resp.getWriter().println(JSONUtil.toJsonStr(r));


    }
    //密保验证
    public void checkStudent(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String studentName = req.getParameter("studentName");
        String question = req.getParameter("question");
        String answer = req.getParameter("answer");
        R r = studentService.checkStudent(studentName, question, answer);
        resp.getWriter().println(JSONUtil.toJsonStr(r));
    }
    public void updatePassword_forgetPassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String token = req.getParameter("token");
        String stuName = req.getParameter("stuName");
        String newPassword = req.getParameter("newPassword");
        //如果TOKEN存在，在失败
        if (token==null||token==""){
            resp.getWriter().println(JSONUtil.toJsonStr(R.fail("信息有误")));
            return;
        }
        //如果token不正确，则失败
        String serverToken = TokenCashUtil.getToken(TokenCashUtil.TOKEN_ + stuName);
        //
        if (serverToken==null){
            resp.getWriter().println(JSONUtil.toJsonStr(R.fail("信息有误")));
            return;
        }
        if (!serverToken.equals(token)){
            resp.getWriter().println(JSONUtil.toJsonStr(R.fail("信息有误")));
            return;
        }
        //修改密码
    }
    public static R questionById;
    public static String studentName;
    //通过账号查找密保信息
    public void getStudentQuestion(HttpServletRequest req, HttpServletResponse resp) throws IOException {
         studentName = req.getParameter("studentName");
         questionById = studentService.getQuestionById(studentName);
        req.getSession().setAttribute("studentName1",studentName);
        resp.setHeader("Content-type", "application/json;charset=utf-8");
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().println(JSONUtil.toJsonStr(questionById));

    }
    public void getStudentQuestion1(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        resp.setHeader("Content-type", "application/json;charset=utf-8");
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().println(JSONUtil.toJsonStr(questionById));

    }
    public void checkQuestionAndAnswer(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String question = req.getParameter("question");
        String answer = req.getParameter("answer");
        R r = studentService.checkQuestionAndAnswer(question, answer, studentName);
        resp.getWriter().println(JSONUtil.toJsonStr(r));


    }
    public void checkStudentName1(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String name = req.getParameter("stuName");
        R r = studentService.queryStudentByStudentName1(name);
        String str = JSONUtil.toJsonStr(r);
        resp.getWriter().println(str);

    }
    public void checkPhone1(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String phone = req.getParameter("phoneNum");
        R r = studentService.queryStudentByPhone1(phone);
        String str = JSONUtil.toJsonStr(r);
        resp.getWriter().println(str);

    }






}
